<?php
namespace Sfsn\Lib;

/**
 * json操作
 * @author shooke
 * class MyClass
{
    public $var1 = 'value的方式 1';
    public $var2 = 'value 2';
    public $var3 = 'value 3';
    protected $protected = 'protected var';
    private   $private   = 'private var';
    function iterateVisible() {
        echo "MyClass::iterateVisible:\n";
        foreach($this as $key => $value) {
            print "$key => $value\n";
        }
    }
}

$class = new MyClass();

$j = Json::encode($class);
print_r($j);
var_dump(Json::toArray($j));
var_dump(Json::toObject($j));
 *
 */
class Json
{
    /**
     * 将对象或数组转为json
     * @param unknown $data
     * @return \Sfsn\Lib\string|string
     */
    public static function encode($data)
    {
        if (version_compare(PHP_VERSION, '5.4.0', '<')) {
            //php版本小于 5.4时进行编码处理
            $data = self::urlencode($data);
            return self::urldecode(json_encode($data));
        } else {
            return json_encode($data, JSON_UNESCAPED_UNICODE);
        }
    }
    /**
     * 将json转为数组
     * @param unknown $data
     * @return \Sfsn\Lib\mixed
     */
    public static function toArray($data)
    {
        return self::decode($data,true);
    }
    /**
     * 将json转为对象
     * @param unknown $data
     * @return \Sfsn\Lib\mixed
     */
    public static function toObject($data)
    {
        return self::decode($data,false);
    }
    /**
     * 将json转换为数组或对象
     * @param string $data
     * @param bool $toArray
     * @return mixed
     */
    public static function decode($data, $toArray=false)
    {
        $return = json_decode($data,$toArray);
        //处理大写字面量TRUE FALSE NULL问题
        if (is_null($return) && version_compare(PHP_VERSION, '5.6.0', '>=')){
            $search = ['TRUE','FALSE','NULL'];
            $replace = ['true','false','null'];
            $data = str_replace($search, $replace, $data);
            $return = json_decode($data,$toArray);
        }
        return $return;

    }

    /**
     * 对数据进行urlencode编码
     * @param unknown $data
     * @return string
     */
    private static function urlencode($data) {
        if (is_array ( $data )) {
            foreach ( $data as $key => $value ) {
                $data [$key] = self::urlencode ( $value );
            }
        }elseif(is_object($data)){
            foreach ( $data as $key => $value ) {
                $data->$key = self::urlencode ( $value );
            }
        } else {
            $data = urlencode ( $data );
        }
        return $data;
    }

    /**
     * 对数据进行urldecode编码
     * @param unknown $data
     * @return string
     */
    private static function urldecode($data) {
        if (is_array ( $data )) {
            foreach ( $data as $key => $value ) {
                $data [$key] = self::urldecode ( $value );
            }
        }elseif(is_object($data)){
            foreach ( $data as $key => $value ) {
                $data->$key = self::urldecode ( $value );
            }
        } else {
            $data = urldecode ( $data );
        }
        return $data;
    }
}
